/******************************************************************************
* Copyright (c) 2007 Alexandru Scvortov
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/


#ifndef COMMON_H
#define COMMON_H

#include <stdio.h>
#include <stdlib.h>

#define check_and_free(x) if (x) { free(x); x = 0; }

#define NEXT(x) x = x->next

char *project_name;
char *project_version;
char *author;
char *site;

enum DocNodeTypes {
    DocNodeUnknown,
    DocNodeText,
    DocNodeReturn,
    DocNodeProjectName
};

enum WordNodeTypes {
    WordNodeUnknown,
    WordNodeName,
    WordNodeFunctionType,
    WordNodeVariableType
};

typedef struct __WN WordNode;
typedef struct __FN FunctionNode;
typedef struct __DN DocNode;
typedef struct __CN CommentNode;
typedef struct __VN VarNode;

struct __DN {
    int type;
    char *value;
    int indent_level;
    DocNode *next;
};

struct __CN {
    DocNode *child;
    CommentNode *next;
    CommentNode *next_for_func;
    int assigned;
};

struct __WN {
    char *value;
    int type;
    WordNode *next;
};

struct __FN {
    WordNode *child;
    FunctionNode *next;
    CommentNode *c_node;
    WordNode *name;
	int line_num;
	char *file_name;
};

struct __VN {
    WordNode *child;
    VarNode *next;
    CommentNode *c_node;
    WordNode *name;
	int line_num;
	char *file_name;
};

//! Performs cleanup actions and exits
void move_out(int ret_code);

//! The maximum number of characters a line can hold. Anything over is read as a separate line
int max_line_length;

//! The file currently beeing scanned
FILE *open_file;

//! The line currently beeing processed
char *current_line;

//! The value of the entity currently beeing processed
char *current_value;

//! Holds the name of the file currently beeing scanned
char *current_file;

//! The number of the line currently beeing scanned
int cur_line_num;

//! Holds the list of comments currentrly found. LILO list
CommentNode *comments;

//! Holds the list of functions currently found. LILO list
FunctionNode *functions;

//! Holds the list of variables currently found. LILO list
VarNode *variables;

/*!
    Scans the specified file for docstrings.

    <docstring> ::= //! <text> | /*! <text> *\/
*/
void scan(char *fn);

/*!
    Goes through a comment extrating relevant information and storing it into
    doc_tree
*/
void parse_comment();

//! Frees all memory used by comment node
CommentNode* comment_node_remove(CommentNode *cn);

//! Frees all memory used by function node
FunctionNode* function_node_remove(FunctionNode *fn);

//! Creates a new word node
WordNode* word_node_new();

//! Frees all memory used by word node
WordNode* word_node_remove(WordNode *wn);

//! Frees all memory used by var node
VarNode* var_node_remove(VarNode *vn);

//! Goes through a function string splitting it into components
void parse_function();

//! Goes through a variable string splitting it into components
void parse_var();

#endif
